import { useRequest } from "ahooks";
import { LoadMoreFormatReturn } from '@ahooksjs/use-request/lib/types';
import { fetchBannerList, fetchSearchCustomer } from "./service";
import { BannerObj, CustomerInfo } from "./type";
import { useState } from "react";
import { useReachBottom } from "@tarojs/taro";

function useData() {
  const [searchObj, setSearchObj] = useState<Partial<CustomerInfo>>()
  const { data: bannerList } = useRequest(fetchBannerList)
  const { data: customerList, loadMore } = useRequest((d) => queryCustomerList(searchObj, d), {
    loadMore: true,
    refreshDeps: [searchObj],
    isNoMore: (r) => !r?.hasMore
  })

  useReachBottom(loadMore)

  return {
    bannerList: bannerData,
    customerList: customerList?.list,
    setSearchObj
  }
}

export default useData

async function queryCustomerList(options?: Partial<CustomerInfo>, d?: LoadMoreFormatReturn) {
  const { current = 0 } = d || { current: 0 };
  const { res } = await fetchSearchCustomer({
    ...options,
    size: 10,
    current,
  });

  if (res) {
    return {
      list: res.records,
      current: res.current + 1,
      hasMore: res.total > res.current * res.size,
    };
  }
  return {
    list: [] as CustomerInfo[],
    hasMore: false,
  };
}
const bannerData: BannerObj[] = [
  {
    id: '1',
    imgUrl: 'https://huolihua-1256524053.cos.ap-nanjing.myqcloud.com/banner1.png',
    bannerColor: '#fff'
  }
]